package com.qeeyou.apps.android.craftsman.activity.publish;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.qeeyou.apps.android.craftsman.R;
import com.qeeyou.apps.android.craftsman.adapter.ImagePreviewAdapter;
import com.qeeyou.apps.android.craftsman.common.Contants;

import java.io.Serializable;
import java.util.List;

public class ImagePreviewActivity extends Activity {

    private Context mContext;

    private ImageView mBtnBack;
    private TextView mTextTitle;
    private TextView mTextDelete;
    private ViewPager viewPager;
    private ImagePreviewAdapter adapter;

    public List<String> drr;
    private int originalSize;

    private int index;
    private boolean isShowDelete;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image_preview);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

        mContext = this;

        setView();
        setEvent();
    }

    private void setView() {
        drr = (List<String>) getIntent().getSerializableExtra(Contants.Extra.EXTRA_IMAGE_LIST);
        index = getIntent().getIntExtra(Contants.Extra.EXTRA_IMAGE_ID, -1);
        isShowDelete = getIntent().getBooleanExtra(Contants.Extra.EXTRA_IMAGE_PREVIEW_IS_SHOW_DELETE, true);

        mBtnBack = (ImageView) findViewById(R.id.btnBack4ImagePreview);
        mTextTitle = (TextView) findViewById(R.id.txtvTitle4ImagePreview);
        mTextDelete = (TextView) findViewById(R.id.txtvDelete4ImagePreview);

        viewPager = (ViewPager) findViewById(R.id.vp_activity_image_preview);
        viewPager.setOnPageChangeListener(pageChangeListener);

        adapter = new ImagePreviewAdapter(mContext, drr);// 构造adapter
        viewPager.setAdapter(adapter);// 设置适配器
        viewPager.setCurrentItem(index);
        mTextTitle.setText(String.format("%d/%d", index + 1, drr.size()));

        if (isShowDelete) {
            mTextDelete.setVisibility(View.VISIBLE);
        } else {
            mTextDelete.setVisibility(View.GONE);
        }

        originalSize = drr.size();
    }

    private void setEvent() {
        mBtnBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (originalSize != drr.size()) {
                    Intent intent = new Intent();
                    intent.putExtra(Contants.Extra.EXTRA_IMAGE_LIST, (Serializable) drr);
                    setResult(RESULT_OK, intent);
                }
                finish();
            }
        });

        mTextDelete.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                drr.remove(index);
                if (drr.size() == 0) {
                    Intent intent = new Intent();
                    intent.putExtra(Contants.Extra.EXTRA_IMAGE_LIST, (Serializable) drr);
                    setResult(RESULT_OK, intent);
                    finish();
                } else {
                    adapter.updateImageInfos(drr);
                }
            }
        });
    }

    private OnPageChangeListener pageChangeListener = new OnPageChangeListener() {

        public void onPageSelected(int position) {// 页面选择响应函数
            index = position;
            mTextTitle.setText(String.format("%d/%d", position + 1, drr.size()));
        }

        public void onPageScrolled(int arg0, float arg1, int arg2) {// 滑动中。。。

        }

        public void onPageScrollStateChanged(int arg0) {// 滑动状态改变

        }
    };
}
